﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RenameTool.CApDataElements
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EMPMODELLib;
using EMPTYPELIBRARYLib;
using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Media;
using Tecnomatix.Engineering;
using Tecnomatix.Planning;
using Tecnomatix.Planning.DotNetFoundation;


namespace DnProcessSimulateCommands.RenameTool;

internal class CApDataElements : INotifyPropertyChanged
{
  private ObservableCollection<CApRenameElement> elements = new ObservableCollection<CApRenameElement>();
  public EmpContext context;
  private DnfClassDef classDef;
  private CheckStatusDelegate checkFunc = new CheckStatusDelegate(CApDataElements.CheckStatus);
  private GetObjectNameFunc getObjectNameFunction;

  private GetObjectNameFunc GetObjectNameFunction
  {
    get
    {
      if (this.getObjectNameFunction == null)
        this.getObjectNameFunction = !TxEmsUtilities.IsInCaptionDisplayNameMode() ? new GetObjectNameFunc(CApDataElements.GetObjectName) : new GetObjectNameFunc(CApDataElements.GetObjectRawName);
      return this.getObjectNameFunction;
    }
  }

  private static string GetObjectName(ITxObject obj) => obj.Name;

  private static string GetObjectRawName(ITxObject obj) => TxObjectEx.GetRawName(obj);

  public CApDataElements() => this.context.sessionId = TxApplication.ActiveDocument.eMSSessionId;

  public ObservableCollection<CApRenameElement> Elements
  {
    get => this.elements;
    set => this.elements = value;
  }

  public static event D ItemWasChecked;

  public static void CheckStatus()
  {
    if (CApDataElements.ItemWasChecked == null)
      return;
    CApDataElements.ItemWasChecked();
  }

  protected void NotifyPropertyChanged(string info)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(info));
  }

  private static ImageSource ConvertBitmapToBitmapImage(Bitmap img)
  {
    try
    {
      return TxImageUtilitiesEx.ImageToImageSource((Image) img);
    }
    catch (Exception ex)
    {
    }
    return (ImageSource) null;
  }

  public virtual CApRenameElement AddRootNode(EmpObjectKey key)
  {
    EmpObject empObject = (EmpObject) new EmpObjectClass();
    CApRenameElement capRenameElement = new CApRenameElement(key, this.context, this.checkFunc);
    this.classDef = DnfClassDef.GetClassDef(((IEmpObject) empObject).GetClassDef(ref this.context, ref key));
    capRenameElement.Icon = CApDataElements.ConvertBitmapToBitmapImage(DnuClassIcons.Instance.GetNodeIcon(ref this.context, ref key));
    capRenameElement.ClassDef = this.classDef;
    this.Elements.Add(capRenameElement);
    return capRenameElement;
  }

  public virtual CApRenameElement AddRootNode(ITxPlanningObject obj)
  {
    EmpObject empObject = (EmpObject) new EmpObjectClass();
    bool success = false;
    EmpObjectKey empObjectKey = this.GetEmpObjectKey(obj, out success);
    CApRenameElement capRenameElement = (CApRenameElement) null;
    if (success)
    {
      capRenameElement = new CApRenameElement(empObjectKey, this.context, this.checkFunc);
      this.classDef = DnfClassDef.GetClassDef(((IEmpObject) empObject).GetClassDef(ref this.context, ref empObjectKey));
      capRenameElement.Icon = CApDataElements.ConvertBitmapToBitmapImage(DnuClassIcons.Instance.GetNodeIcon(ref this.context, ref empObjectKey));
      capRenameElement.Obj = (ITxObject) obj;
      capRenameElement.ClassDef = this.classDef;
      this.Elements.Add(capRenameElement);
    }
    return capRenameElement;
  }

  public virtual CApRenameElement AddRootNode(ITxObject obj)
  {
    CApRenameElement capRenameElement = new CApRenameElement(obj, this.checkFunc, this.GetObjectNameFunction);
    capRenameElement.Icon = TxIconManager.the().GetObjectImageSource(obj, true);
    this.Elements.Add(capRenameElement);
    return capRenameElement;
  }

  public virtual void AddRootNode(CApRenameElement obj) => this.Elements.Add(obj);

  public void Move(int oldIndex, int newIndex) => this.elements.Move(oldIndex, newIndex);

  public bool Contains(EmpObjectKey key)
  {
    for (int index = 0; index < this.Elements.Count; ++index)
    {
      if (this.Elements[index].Key.objectId == key.objectId)
        return true;
    }
    return false;
  }

  public bool Contains(ITxObject key)
  {
    for (int index = 0; index < this.Elements.Count; ++index)
    {
      try
      {
        if (this.Elements[index].Obj.Id == key.Id)
          return true;
      }
      catch (Exception ex)
      {
      }
    }
    return false;
  }

  public EmpObjectKey GetEmpObjectKey(ITxPlanningObject planObj, out bool success)
  {
    EmpObjectKey empObjectKey = new EmpObjectKey();
    empObjectKey.objectId = -1;
    if (planObj != null)
    {
      ITxEmsServicesProvider servicesProvider = (ITxEmsServicesProvider) planObj.PlatformServicesProvider;
      empObjectKey.objectId = servicesProvider.InternalId;
      if (empObjectKey.objectId == 0)
      {
        success = false;
        return empObjectKey;
      }
      success = true;
      return empObjectKey;
    }
    success = false;
    return empObjectKey;
  }

  public event PropertyChangedEventHandler PropertyChanged;

  public Tuple<EmpObjectKey[], string[]> GetCheckedObjectsAndAttributes(List<string> attributes)
  {
    HashSet<EmpObjectKey> source1 = new HashSet<EmpObjectKey>();
    HashSet<string> source2 = new HashSet<string>();
    for (int index = 0; index < this.Elements.Count; ++index)
    {
      if (this.Elements[index].IsChecked)
      {
        foreach (string attribute in attributes)
        {
          if (!this.Elements[index].AttributeValue.ContainsKey(attribute))
          {
            source2.Add(attribute);
            source1.Add(this.Elements[index].Key);
          }
        }
      }
    }
    return new Tuple<EmpObjectKey[], string[]>(source1.ToArray<EmpObjectKey>(), source2.ToArray<string>());
  }

  public void UpdateAttributes(EmpFieldInfo[] fieldInfos, string[] requestedAttributes)
  {
    foreach (CApRenameElement element in (Collection<CApRenameElement>) this.Elements)
    {
      if (element.IsChecked)
      {
        foreach (string requestedAttribute in requestedAttributes)
        {
          if (!element.AttributeValue.ContainsKey(requestedAttribute))
            element.AttributeValue.Add(requestedAttribute, (string) null);
          for (int index = 0; index < fieldInfos.Length; ++index)
          {
            if (element.Key.objectId == fieldInfos[index].NodeKey.objectId && requestedAttribute == fieldInfos[index].FieldKey.fieldName && fieldInfos[index].FieldType == 1 && fieldInfos[index].Value != null)
            {
              element.AttributeValue[fieldInfos[index].FieldKey.fieldName] = fieldInfos[index].Value.ToString();
              break;
            }
          }
        }
      }
    }
  }

  public List<Dictionary<string, string>> GetAttributesValues(
    List<string> attributes,
    out IEnumerable<string> wrongAttributes)
  {
    List<Dictionary<string, string>> attributesValues = new List<Dictionary<string, string>>();
    HashSet<string> second = new HashSet<string>();
    for (int index = 0; index < this.Elements.Count; ++index)
    {
      if (this.Elements[index].IsChecked)
      {
        Dictionary<string, string> dictionary = new Dictionary<string, string>();
        foreach (string attribute in attributes)
        {
          if (this.Elements[index].AttributeValue.ContainsKey(attribute) && !dictionary.ContainsKey(attribute) && this.Elements[index].AttributeValue[attribute] != null)
          {
            dictionary.Add(attribute, this.Elements[index].AttributeValue[attribute]);
            second.Add(attribute);
          }
        }
        attributesValues.Add(dictionary);
      }
    }
    wrongAttributes = attributes.Except<string>((IEnumerable<string>) second);
    return attributesValues;
  }
}
